
/*
 *
 * Copyright (C) 2009 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

using FlowLib.Containers;
using FlowLib.Events;
using ClientExample.Comparers;

namespace ClientExample.Containers
{
    public class AppSetting
    {
        public event FmdcEventHandler SettingsUpdated;

        public AppSetting()
        {
            SettingsUpdated = new FmdcEventHandler(AppSetting_SettingsUpdated);
			SavedHubs = new List<HubSetting>(0);
            SavedFriends = new FlowSortedList<Friend>(new ClientExample.Comparers.FriendComparer(ClientExample.Comparers.UserInfoComparer.Columns.StoreId));
        }

        void AppSetting_SettingsUpdated(object sender, FmdcEventArgs e) { }

        protected bool bUpdateSequence = false;
		protected string sVersion;
        protected bool bInstalled;

        protected List<HubSetting> lSavedHubs;
        protected FlowSortedList<Friend> lFriends;
        protected int iConnectionMode;
        protected string sIP;
        protected string sInternalIP;
        protected int iTcpPort;
        protected int iSearchType;
        protected string sPID;
        protected bool bDetectURLs = true;
		protected int iMsgNotityStatus = 0;
        protected bool bShowDateStamp = true;
        protected string sDateStampFormat = "yyyy-MM-dd HH:mm:ss";
        protected string sImagePath = null;
        protected string sHubsPath = null;
        protected string sFriendsPath = null;
        protected string sSharesPath = null;
        protected string sDownloadPath = null;
        // Locations
        protected int iLocationType;
        protected Point pMainWindowLocation;
        protected Size sMainWindowSize;
        protected Point pMsgWindowLocation;
        protected Size sMsgWindowSize;
        protected Point pSchResultWindowLocation;
        protected Size sSchResultWindowSize;
        protected Point pSchExtWindowLocation;
        protected Size sSchExtWindowSize;
        protected bool bUninstall = false;
        protected bool bDebugMsg = false;

        public string SharesPath
        {
            get
            {
                if (sSharesPath == null)
                {
                    return Program.Paths.Settings;
                }
                return sSharesPath;
            }
            set
            {
                sSharesPath = value;
                ValueUpdated();
            }
        }

        public string FriendsPath
        {
            get
            {
                if (sFriendsPath == null)
                {
                    return Program.Paths.Settings + "Friends.xml";
                }
                return sFriendsPath;
            }
            set
            {
                sFriendsPath = value;
                ValueUpdated();
            }
        }

        public string HubPath
        {
            get
            {
                if (sHubsPath == null)
                {
                    return Program.Paths.Settings + "Favorites.xml";
                }
                return sHubsPath;
            }
            set
            {
                sHubsPath = value;
                ValueUpdated();
            }
        }

        public string ImagePath
        {
            get
            {
                if (sImagePath == null)
                {
                    if (IsRunningOnMono)
                        return "Images.Mono.";
                    else
                        return "Images.dotNet.";
                }
                return sImagePath;
            }
            set
            {
                sImagePath = value;
                ValueUpdated();
            }
        }

        public bool ShowDateStamp
        {
            get { return bShowDateStamp; }
            set
            {
                bShowDateStamp = value;
                ValueUpdated();
            }
        }

        public string DateStampFormat
        {
            get { return sDateStampFormat; }
            set
            {
                sDateStampFormat = value;
                ValueUpdated();
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public static bool IsRunningOnMono
        {
            get { return (Type.GetType("Mono.Runtime") != null); }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool HaveUnSavedInfo
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool IsWriting
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool IsReading
        {
            get;
            set;
        }
        
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool Uninstall
        {
            get { return bUninstall; }
            set { bUninstall = value; }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool UpdateSequence
        {
            get { return bUpdateSequence; }
            set
            {
                bUpdateSequence = value;
                ValueUpdated();
            }
        }

        public void ValueUpdated()
        {
            HaveUnSavedInfo = true;
            if (!bUpdateSequence)
                SettingsUpdated(this, new FmdcEventArgs(0, null));
        }

        public string PID
        {
            get { return sPID; }
            set
            {
                sPID = value;
                ValueUpdated();
            }
        }
		
        public string Version
        {
            get { return sVersion;}
            set 
            {
                sVersion = value;
                ValueUpdated();
            }
        }
		
        public bool Installed
        {
            get { return bInstalled; }
            set
            {
                bInstalled = value;
                ValueUpdated();
            }
        }
        public bool DebugMsg
        {
            get { return bDebugMsg; }
            set
            {
                bDebugMsg = value;
                ValueUpdated();
            }
        }
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public List<HubSetting> SavedHubs
        {
            get { return lSavedHubs; }
            set
            {
                lSavedHubs = value;
                ValueUpdated();
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public FlowSortedList<Friend> SavedFriends
        {
            get { return lFriends; }
            set
            {
                if (lFriends != null)
                {
                    lFriends.ItemAdded -= lFriends_ItemAdded;
                    lFriends.ItemRemoved -= lFriends_ItemRemoved;
                }
                lFriends = value;
                lFriends.Sort(new FriendComparer(UserInfoComparer.Columns.StoreId));
                lFriends.TriggerEvents = true;
                lFriends.ItemAdded += new FlowSortedList<Friend>.SingleChangedDelegate(lFriends_ItemAdded);
                lFriends.ItemRemoved += new FlowSortedList<Friend>.SingleChangedDelegate(lFriends_ItemRemoved);
                ValueUpdated();
            }
        }

        void lFriends_ItemRemoved(int pos, Friend item)
        {
            ValueUpdated();
        }

        void lFriends_ItemAdded(int pos, Friend item)
        {
            ValueUpdated();
        }

        /// <value>
		/// 0 = No general notification
		/// 1 = System sound
		/// 2 = Notify Window
		/// 4 = Icon change
		/// </value>
        public int NotityMsgStatus
        {
            get { return iMsgNotityStatus; }
            set
            {
                iMsgNotityStatus = value;
                ValueUpdated();
            }
        }
		/// <value>
		/// 0 = No Internet connection
		/// 1 = Passive connection
		/// 2 = Active connection
		/// </value>
        public int ConnectionMode
        {
            get { return iConnectionMode; }
            set
            {
                iConnectionMode = value;
                ValueUpdated();
            }
        }
        public string IP
        {
            get { return sIP; }
            set
            {
                sIP = value;
                ValueUpdated();
            }
        }

        public string InternalIP
        {
            get { return sInternalIP; }
            set
            {
                sInternalIP = value;
                ValueUpdated();
            }
        }
        /// <value>
		/// Default is 6000
		/// </value>
        public int TcpPort
        {
            get { return iTcpPort; }
            set
            {
                iTcpPort = value;
                ValueUpdated();
            }
        }
		/// <value>
		/// 0 = Content
		/// 1 = Users
		/// 2 = Search Spy
		/// </value>
        public int SearchType
        {
            get { return iSearchType; }
            set 
            {
                iSearchType = value;
                ValueUpdated();
            }
        }
		/// <value>
		/// 0 = Absolute
		/// 1 = Relative
		/// </value>
        public int LocationType
        {
            get { return iLocationType; }
            set
            {
                iLocationType = value;
                ValueUpdated();
            }
        }
		/// <value>
		/// Makes links clickable and blue =)
		/// </value>
        public bool DetectUrlInMessage
        {
            get { return bDetectURLs; }
            set
            {
                bDetectURLs = value;
                ValueUpdated();
            }
        }

        #region Main Window
        public Point MainWindowLocation
        {
            get { return pMainWindowLocation; }
            set
            {
                pMainWindowLocation = value;
                ValueUpdated();
            }
        }
        public Size MainWindowSize
        {
            get { return sMainWindowSize; }
            set
            {
                sMainWindowSize = value;
                ValueUpdated();
            }
        }
        #endregion
        #region Msg Window
        public Point MsgWindowLocation
        {
            get { return pMsgWindowLocation; }
            set
            {
                pMsgWindowLocation = value;
                ValueUpdated();
            }
        }
        public Size MsgWindowSize
        {
            get { return sMsgWindowSize; }
            set
            {
                sMsgWindowSize = value;
                ValueUpdated();
            }
        }
        #endregion
        #region Search Result Window
        public Point SchResultWindowLocation
        {
            get { return pSchResultWindowLocation; }
            set
            {
                pSchResultWindowLocation = value;
                ValueUpdated();
            }
        }
        public Size SchResultWindowSize
        {
            get { return sSchResultWindowSize; }
            set
            {
                sSchResultWindowSize = value;
                ValueUpdated();
            }
        }
        #endregion
        #region Search Ext Window
        public Point SchExtWindowLocation
        {
            get { return pSchExtWindowLocation; }
            set
            {
                pSchExtWindowLocation = value;
                ValueUpdated();
            }
        }
        public Size SchExtWindowSize
        {
            get { return sSchExtWindowSize; }
            set
            {
                sSchExtWindowSize = value;
                ValueUpdated();
            }
        }
        #endregion
    }
}
